package com.bzs.batch.reconcile.service.impl;

import com.bzs.batch.reconcile.entity.HostTransactionflow;
import com.bzs.batch.reconcile.batch.job.ConJob;
import com.bzs.batch.reconcile.mapper.HostTransactionflowMapper;
import com.bzs.batch.reconcile.service.IHostTransactionflowService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.AllArgsConstructor;
import org.springframework.batch.core.JobParameters;
import org.springframework.batch.core.JobParametersBuilder;
import org.springframework.batch.core.JobParametersInvalidException;
import org.springframework.batch.core.launch.JobLauncher;
import org.springframework.batch.core.repository.JobExecutionAlreadyRunningException;
import org.springframework.batch.core.repository.JobInstanceAlreadyCompleteException;
import org.springframework.batch.core.repository.JobRestartException;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author baozs
 * @since 2021-12-21
 */
@Service
@AllArgsConstructor
public class HostTransactionflowServiceImpl extends ServiceImpl<HostTransactionflowMapper, HostTransactionflow> implements IHostTransactionflowService {

    private final JobLauncher jobLauncher;
    private final ConJob conJob;

    @Override
    public void reconcile() {
        try {
            //批处理框架运行需要的参数，需要传一个参数为“job的主键”,保证任务不重复,达到重复执行任务的目的
            JobParameters jobParameters = new JobParametersBuilder()
                    .addDate("date", new Date())
                    .toJobParameters();
            jobLauncher.run(conJob.getAConJob(),jobParameters);
        } catch (JobInstanceAlreadyCompleteException | JobExecutionAlreadyRunningException | JobParametersInvalidException | JobRestartException e) {
            e.printStackTrace();
        }

    }
}
